#include "manage_stuinfo.h"
#include "ui_manage_stuinfo.h"

manage_stuinfo::manage_stuinfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::manage_stuinfo),
    newAddStuface(new addStuface),
    newRemoveStuface(new removeStuface),
    newModifystuface(new modifyStuface)
{
    ui->setupUi(this);

    appDir = QCoreApplication::applicationDirPath();

    /* 创建模型 */
    createModel();

    /* 加载学生信息 */
    loadstuinformation();

    connect(newAddStuface, &addStuface::updataView, this, &manage_stuinfo::updateView);
    connect(newRemoveStuface, &removeStuface::updataView, this, &manage_stuinfo::updateView);
    connect(newModifystuface, &modifyStuface::updataView, this, &manage_stuinfo::updateView);
}

/* 更新列表 */
void manage_stuinfo::updateView()
{
    clearView();

    QFile file(appDir + "/stuInfo.txt");
    if (file.exists() == false)
    {
        qDebug() << "创建stuInfo文件" << endl;
    }

    file.open(QIODevice::ReadOnly);

    int lineCount = 0;
    char lineBuf[128] = {0};    //读取内容缓存区域
    while (file.atEnd() == false) // 逐行读取文件内容并计数
    {
        file.readLine(lineBuf, sizeof(lineBuf));
        lineCount++;

        QString data = lineBuf;

        /* 添加一行 */
        int row = model->rowCount();
        model->insertRow(row);  // 插入新的一行

        // 单独设置每个单元格的数据
        QString name = data.split("+")[0];
        QStandardItem *nameItem = new QStandardItem(name);
        model->setItem(row, 0, nameItem);

        QString id = data.split("+")[1];
        QStandardItem *idItem = new QStandardItem(id);
        model->setItem(row, 1, idItem);

        QString password = data.split("+")[2];
        QStandardItem *passwordItem = new QStandardItem(password);
        model->setItem(row, 2, passwordItem);

        QString cnt = data.split("+")[3].split("\n")[0];
        QStandardItem *cntItem = new QStandardItem(cnt);
        model->setItem(row, 3, cntItem);
    }

    file.close();
}

/* 新建学生信息 */
void manage_stuinfo::on_addBtn_clicked()
{
    newAddStuface->show();
}

/* 删除学生信息 */
void manage_stuinfo::on_removeBtn_clicked()
{
    newRemoveStuface->show();
}

/* 修改学生信息 */
void manage_stuinfo::on_modifyBtn_clicked()
{
    newModifystuface->show();
}

/* 创建模型 */
void manage_stuinfo::createModel()
{
    model = new QStandardItemModel(0, 4);
    model->setHorizontalHeaderItem(0, new QStandardItem("姓名"));
    model->setHorizontalHeaderItem(1, new QStandardItem("账号"));
    model->setHorizontalHeaderItem(2, new QStandardItem("密码"));
    model->setHorizontalHeaderItem(3, new QStandardItem("借阅书籍的数量"));

    ui->tableView->setModel(model);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

/* 加载学生信息 */
void manage_stuinfo::loadstuinformation()
{
    QFile file(appDir + "/stuInfo.txt");
    if (file.exists() == false)
    {
        qDebug() << "创建stuInfo文件" << endl;
    }

    file.open(QIODevice::ReadOnly);

    int lineCount = 0;
    char lineBuf[128] = {0};    //读取内容缓存区域
    while (file.atEnd() == false) // 逐行读取文件内容并计数
    {
        /* 添加一行 */
        int row = model->rowCount();
        model->insertRow(row);  // 插入新的一行

        file.readLine(lineBuf, sizeof(lineBuf));
        QString data = lineBuf;

        QString name = data.split("+")[0];
        QStandardItem *nameItem = new QStandardItem(name);
        model->setItem(lineCount, 0, nameItem);

        QString id = data.split("+")[1];
        QStandardItem *idItem = new QStandardItem(id);
        model->setItem(lineCount, 1, idItem);

        QString password = data.split("+")[2];
        QStandardItem *passwordItem = new QStandardItem(password);
        model->setItem(lineCount, 2, passwordItem);

        QString cnt = data.split("+")[3].split("\n")[0];
        QStandardItem *cntItem = new QStandardItem(cnt);
        model->setItem(lineCount, 3, cntItem);

        lineCount++;
    }
    file.close();
}

/* 清空表格数据 */
void manage_stuinfo::clearView()
{
    int rowCount = model->rowCount();
    int columnCount = model->columnCount();

    // 清除所有单元格的内容
    for (int row = 0; row < rowCount; ++row)
    {
        for (int col = 0; col < columnCount; ++col)
        {
            if (model->item(row, col))
            {
                model->setItem(row, col, new QStandardItem());
            }
        }
    }

    // 如果有必要，可以调整行数到零
    model->setRowCount(0);
}

manage_stuinfo::~manage_stuinfo()
{
    delete ui;
}

